#include "myplot.h"

myPlot::myPlot() : QwtPlot()
{
        this->setAxisScale(0,-100,100);
        this->setAxisAutoScale(QwtPlot::yLeft);

        this->grid = new QwtPlotGrid();
        this->grid->setMajPen(QPen(Qt::gray, 0, Qt::DotLine));
        this->grid->setMinPen(QPen(Qt::gray, 0 , Qt::DotLine));
        this->grid->attach(this);

        this->picker = new QwtPlotPicker(
                    QwtPlot::xBottom,
                    QwtPlot::yLeft,
                    QwtPicker::PolygonSelection,
                    QwtPlotPicker::PolygonRubberBand,
                    QwtPicker::AlwaysOn,
                    this->canvas());

        this->picker->setMousePattern(QwtEventPattern::MouseSelect1,Qt::LeftButton, Qt::CTRL);

        this->zoomer = new  QwtPlotZoomer(
                            QwtPlot::xBottom,
                            QwtPlot::yLeft,
                            QwtPicker::DragSelection,
                            QwtPicker::AlwaysOff,
                            this->canvas());

        connect(this->zoomer, SIGNAL(zoomed(const QwtDoubleRect&)),this,SLOT(zoomed()));


        this->setAxisTitle(QwtPlot::xBottom, "Tempo(s)");
        this->setAxisTitle(QwtPlot::yLeft, "Amplitude");
}

void myPlot::zoomed () {
    if (this->zoomer->zoomRectIndex() == 0) {
            this->setAxisAutoScale(QwtPlot::xBottom);
            this->setAxisAutoScale(QwtPlot::yLeft);
            this->replot();
            this->zoomer->setZoomBase();
        }

}

void myPlot::exportar( const QString &nome_arquivo )
{
    // Considera-se que todas as extensoes dos arquivos possuem 3 caracteres.
    QString extensao = nome_arquivo.right( 3 );

    QwtPlotPrintFilter filtro;

    int opcoes = ( QwtPlotPrintFilter::PrintAll &
                   ~QwtPlotPrintFilter::PrintBackground ) |
                 QwtPlotPrintFilter::PrintFrameWithScales;

    filtro.setOptions( opcoes );

    if ( extensao == "svg" )
    {
        QSvgGenerator gerador;

        gerador.setFileName( nome_arquivo );
        gerador.setSize( this->size() );

        this->print( gerador, filtro );
    }
    else if ( extensao == "pdf" || extensao == "ps" )
    {
    }
}
